package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishesMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.ISetmealService;
import com.sky.service.SetmealDishesService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐模块相关接口
 *
 * @Author mr.Du
 * @Date 2025/5/21 2025/5/21
 */
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private ISetmealService setmealService;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishesService setmealDishesService;
    @Autowired
    private SetmealDishesMapper setmealDishesMapper;

    /**
     * 新增套餐
     *
     * @param dto
     * @return
     */
    @PostMapping
    public Result addSetmeal(@RequestBody SetmealDTO dto) {
        setmealService.addSetmeal(dto);
        return Result.success();
    }

    /**
     * 分页查询套餐列表
     *
     * @param dto
     * @return
     */
    @GetMapping("/page")
    public Result pageSetmeal(SetmealPageQueryDTO dto) {
        PageResult pageResult = setmealService.pageSetmeal(dto);
        return Result.success(pageResult);
    }

    /**
     * 根据id批量删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result deleteSet(@RequestParam("ids") List<Integer> ids) {
        setmealMapper.deleteBatchIds(ids);
        List<SetmealDish> setmealDishes = setmealDishesService.lambdaQuery()
                .in(SetmealDish::getSetmealId, ids).list();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDishesMapper.deleteById(setmealDish.getId());
        }
        return Result.success();
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        SetmealVO setmealVO = setmealService.findById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐信息
     *
     * @param dto
     * @return
     */
    @PutMapping
    public Result updateSetmeal(@RequestBody SetmealDTO dto) {
        setmealService.updateSetmeal(dto);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable("status") Integer status, Integer id) {
        setmealService.lambdaUpdate()
                .eq(Setmeal::getId, id)
                .set(Setmeal::getStatus, status)
                .update();
        return Result.success();
    }
}
